﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class GroupInformationController : Controller
    {
        private GroupInformationInfo groinfo = new GroupInformationInfo();
        public ActionResult GroupInformationIndex()
        {
            ViewData["deptName"] = GetdeptName("全部");
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName(string str)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = groinfo.GetdeptName();
            if (str == "全部")
            {
                dic.Add(-1, "全部");
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetGroupList(PageVM pvm)
        {
            string query = " and 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                if (param[0] != "")
                {
                    query += " and a.groupName like '%" + param[0] + "%'";
                }
                if (param[1] != "-1")
                {
                    query += " and a.departmentId='" + param[1] + "'";
                }
            }
            int count;
            IList<GroupInformationInfo> grolist = groinfo.SelGroupInfo(pvm.rp, pvm.page, query, out count);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = count;
            List<string> v;
            foreach (GroupInformationInfo item in grolist)
            {
                #region
                v = new List<string>();
                v.Add(item.groupId.ToString());
                v.Add(item.groupName);
                v.Add(item.departname);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.groupId.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        public ActionResult AddGroupInformation()
        {
            ViewData["deptName"] = GetdeptName("");
            return View();
        }

        /// <summary>
        /// 保存班组（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGroupInformation(GroupInformationVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                GroupInformationInfo gro = new GroupInformationInfo()
                {
                    groupName = model.groupName,
                    departmentId = model.departmentId
                };
                result = groinfo.SaveGroupInformation(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditGroupInformation(string id)
        {
            ViewData["deptName"] = GetdeptName("");
            GroupInformationVM vm = new GroupInformationVM();
            GroupInformationInfo info = new GroupInformationInfo();
            groinfo = info.GetGroupInformationInfoOne(id);
            vm.groupId = groinfo.groupId;
            vm.groupName = groinfo.groupName;
            vm.departmentId = groinfo.departmentId;
            //Mapper.DynamicMap(groinfo, vm);
            return View(vm);
        }

        /// <summary>
        /// 保存班组（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGroupInformationEdit(GroupInformationVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                GroupInformationInfo gro = new GroupInformationInfo()
                {
                    groupId = model.groupId,
                    groupName = model.groupName,
                    departmentId = model.departmentId
                };
                result = groinfo.SaveGroupInformationEdit(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteGroupInformation(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = groinfo.DeleteGroupInformation(strId);
            return Json(result);
        }
    }
}
